// pages/vipCore/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
const defaultAvatarUrl = 'https://lhs.tann.cc/upload/pic/public/tx.jpg'
var last_scroll_position = 0
var new_scroll_position = 0
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navHeight: app.globalData.barInfo.navHeight, //导航栏高度
        defaultAvatarUrl: defaultAvatarUrl,
        show: false,
        kefu: wx.getStorageSync('kefu') ? wx.getStorageSync('kefu') : '',
        is_write_style: true,
        index: 0,
        is_shop: wx.getStorageSync('is_shop'),
        meIsShow: true,
        tabIndex: 1,
        topOpacity: 0,
        uid: wx.getStorageSync('user_id3') || '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        app.editTabbar();
    },

    onPageScroll: function (e) {
        // Do something when page scroll
        // console.log('我动了吗')
        // console.log(e, '滑动');
        if (e.scrollTop > 50) {
            let opacity = e.scrollTop / 300;
            opacity = opacity > 1 ? 1 : opacity;
            this.setData({
                topOpacity: opacity
            });
        } else {
            this.setData({
                topOpacity: 0
            });
        }

        var that = this
        let write_style = that.data.write_style
        last_scroll_position = e.scrollTop;
        if (new_scroll_position < last_scroll_position && last_scroll_position > 50) {
            that.setData({
                write_style: 'slideUp'
            })

        } else {
            that.setData({
                write_style: 'slideDown'
            })
        }
        new_scroll_position = last_scroll_position;
        let floatingFrame = this.selectComponent('#floatingFrame'); // 页面获取自定义组件实例
        floatingFrame.isHide(); // 通过实例调用组件事件
        if (that.data.write_style != write_style && that.data.write_style == 'slideDown') {
            // console.log('我动了吗')
            that.setData({
                is_write_style: true
            })


        }
        if (that.data.write_style != write_style && that.data.write_style == 'slideUp') {
            that.setData({
                is_write_style: false
            })


        }
    },

    changeTabBtn(e) {
        const {
            t
        } = e.currentTarget.dataset

        wx.showToast({
            title: '文化馆尚未开放，请耐心等待',
            icon: 'none'
        })

        return

        this.setData({
            tabIndex: t
        })
    },

    swiperC(e) {
        console.log(e.detail.current);
        this.setData({
            index: e.detail.current
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 获取用户信息
     */
    getUserInfo: function () {
        console.log('show')
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_user/' + paraArr['id'],
            data: {
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                console.log(res.data, '1111111111');
                that.setData({
                    nickname: res.data.data.nickname,
                    avatar: res.data.data.avatar,
                    mobile: res.data.data.mobile,
                    is_shop: res.data.data.is_shop,
                    level: res.data.data.level_id,
                    user_id: res.data.data.user_id,
                    is_vip: res.data.data.is_vip,
                    hiddenLoading: true,
                    vip_express_time: res.data.data.vip_express_time
                });
                wx.setStorageSync('is_vip', res.data.data.is_vip)
                wx.setStorageSync('owner_shop_name', res.data.data.owner_shop_name)
                if (!res.data.data.avatar || !res.data.data.nickname) {
                    console.log('???')
                    that.setData({
                        meIsShow: false
                    })
                    wx.setStorageSync('meIsShow', that.data.meIsShow)
                } else {
                    console.log('???11')
                    that.setData({
                        meIsShow: true
                    })
                    wx.setStorageSync('meIsShow', true)
                }
            }
        })
    },

    onClickShow() {
        this.setData({
            show: true
        });
    },

    onClickHide() {
        console.log('9999')
        this.setData({
            show: false
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        // let meIsShow = wx.getStorageSync('meIsShow')
        let kefu = wx.getStorageSync('kefu')
        if (wx.getStorageSync('user_id3')) {
            app.login(function (userInfo) {
                that.setData({
                    userInfo: userInfo,
                    uid: wx.getStorageSync('user_id3'),
                    kefu: kefu
                });
                that.getUserInfo();
            });
        }
    },

    goLoginBtn() {
        let that = this
        let kefu = wx.getStorageSync('kefu')
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo,
                uid: wx.getStorageSync('user_id3'),
                kefu: kefu
            });
            that.getUserInfo();
        });
    },

    // 输入/修改昵称
    clickPopup() {
        // let arr = ['J8xfVd-OfPgGZvrvcZUmVZPalU0LJfvCuemjzrVkpAs', 'GZQNbaOX4wp9toy-Go3QhyUg4VQEj-rt-lryVqBmHjc']
        // wx.requestSubscribeMessage({
        //   tmplIds: arr, // 最多支持3条
        //   success(res) {
        //     console.log(res, '0099090090');
        //     // 'accept'表示用户同意订阅该条id对应的模板消息
        //     // if (res[''] === 'accept') {
        //     //   // 用户同意订阅，调用云函数或服务器接口发送订阅消息

        //     // }
        //   },
        //   fail(err) {
        //     console.log(err, '88888888');
        //   }
        // })

        let that = this
        wx.showModal({
            title: '设置昵称',
            placeholderText: '请输入您的昵称',
            editable: true,
            confirmColor: '#ff0000',
            success(res) {
                var paraArr = new Array();
                var sign = app.signature(paraArr);
                paraArr['user_id'] = that.data.userInfo.userID;
                if (res.confirm) {
                    console.log(res, '用户点击确定')
                    that.setData({
                        nickName: res.content
                    })
                    wx.request({
                        url: rootDocment + '/api/user/z_save_nickname',
                        method: 'POST',
                        data: {
                            id: paraArr['user_id'],
                            nickname: res.content,
                            sign: sign
                        },
                        success(val) {
                            that.getUserInfo();
                        }
                    })
                    wx.setStorageSync('nickName', res.content)
                }
            }
        })
    },

    // 头像
    onChooseAvatar(e) {
        let that = this
        const {
            avatarUrl
        } = e.detail
        that.setData({
            avatar: avatarUrl,
        })
        wx.setStorageSync('avatarUrl', that.data.avatar)
        wx.uploadFile({
            url: rootDocment + '/api/user/z_upload',
            name: 'up_file',
            filePath: avatarUrl,
            header: {},
            formData: {
                up_file: that.data.avatar,
            },
            success: (res) => {
                var paraArr = new Array();
                var sign = app.signature(paraArr);
                const url = JSON.parse(res.data)
                console.log(url)
                that.setData({
                    avatar: url.data.image_url
                })
                wx.setStorageSync('userImage', url.data.image_url)
                var paraArr = new Array();
                paraArr['user_id'] = that.data.userInfo.userID;
                wx.request({
                    url: rootDocment + '/api/user/z_save_avatar',
                    method: "POST",
                    data: {
                        avatar: url.data.image_url,
                        id: paraArr['user_id'],
                        sign: sign
                    },
                    success(res) {
                        that.getUserInfo();
                    }
                })

            }
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        console.log(res);
        var that = this;
        return {
            title: '榴皇树',
            path: '/pages/index/index?scene=' + wx.getStorageSync('user_id3')
        }
    },

})